package core.form.election;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

public class WzdPgAddEditVotingCard extends WizardPage {
//	private DataBindingContext m_bindingContext;
//	
//	public DataBindingContext getBindingContext() {
//		return m_bindingContext;
//	}
	
	private boolean addVotingCard;
	private VotingCard votingCard;
	
	private Spinner checksAllowedSpinner;
	private Text descriptionText;
	private Text nameText;

	/**
	 * Create the wizard.
	 * 
	 * @param votingCard
	 * @param addVotingCard
	 */
	public WzdPgAddEditVotingCard(boolean addVotingCard, VotingCard votingCard) {
		super("WzdPgAddEditVotingCard");
		if (addVotingCard) {
			setTitle("Add Voting Card");
			setDescription("Add new Voting Card to the Election.");
		} else {
			setTitle("Edit Voting Card");
			setDescription("Edit an existing Voting Card of the Election.");
		}
		
		this.addVotingCard = addVotingCard;
		this.votingCard = votingCard;
	}

	/**
	 * Create contents of the wizard.
	 * 
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);

		container.setLayout(new GridLayout(2, false));

		new Label(container, SWT.NONE).setText("ChecksAllowed:");

		checksAllowedSpinner = new Spinner(container, SWT.BORDER);
		checksAllowedSpinner.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		new Label(container, SWT.NONE).setText("Description:");

		descriptionText = new Text(container, SWT.BORDER | SWT.WRAP | SWT.MULTI);
		GridData gd_descriptionText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_descriptionText.heightHint = 95;
		descriptionText.setLayoutData(gd_descriptionText);

		new Label(container, SWT.NONE).setText("Name:");

		nameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

//		if (votingCard != null) {
//			m_bindingContext = initDataBindings();
//		}
//		WizardPageSupport.create(this, m_bindingContext);
	}
	
	/*private DataBindingContext initDataBindings() {
		final IObservableValue checksAllowedObserveWidget = SWTObservables.observeSelection(checksAllowedSpinner);
		IObservableValue checksAllowedObserveValue = PojoObservables.observeValue(votingCard, "checksAllowed");
		IObservableValue descriptionObserveWidget = SWTObservables.observeText(descriptionText, SWT.Modify);
		IObservableValue descriptionObserveValue = PojoObservables.observeValue(votingCard, "description");
		final IObservableValue nameObserveWidget = SWTObservables.observeText(nameText, SWT.Modify);
		IObservableValue nameObserveValue = PojoObservables.observeValue(votingCard, "name");
		//
		DataBindingContext bindingContext = new DataBindingContext();
		//
		bindingContext.bindValue(checksAllowedObserveWidget, checksAllowedObserveValue, null, null);
		bindingContext.bindValue(descriptionObserveWidget, descriptionObserveValue, null, null);
		bindingContext.bindValue(nameObserveWidget, nameObserveValue, null, null);
		//
		
		MultiValidator multiValidator = new MultiValidator() {
			protected IStatus validate() {
				Integer checksAllowed = Integer.valueOf(checksAllowedObserveWidget.getValue().toString());
				String name = (String) nameObserveWidget.getValue();

				// error
				if(checksAllowed <= 0) {
					return ValidationStatus.error("Checks allowed value cannot must be positive.");
				}
				if (name.trim().length() == 0) {
					return ValidationStatus.error("Name cannot be empty.");
				}
				// ok
				return ValidationStatus.ok();
			}
		};

		bindingContext.addValidationStatusProvider(multiValidator);
		return bindingContext;
	}*/

	public core.form.election.VotingCard getVotingCard() {
		return votingCard;
	}

	public void setVotingCard(core.form.election.VotingCard newVotingCard) {
		setVotingCard(newVotingCard, true);
	}

	public void setVotingCard(core.form.election.VotingCard newVotingCard, boolean update) {
		votingCard = newVotingCard;
//		if (update) {
//			if (m_bindingContext != null) {
//				m_bindingContext.dispose();
//				m_bindingContext = null;
//			}
//			if (votingCard != null) {
//				m_bindingContext = initDataBindings();
//			}
//		}
	}

}
